package com.shark.erp.service;

import com.alibaba.fastjson.JSONObject;
import com.shark.erp.entity.Purchase;
import com.shark.erp.entity.PurchaseDetail;
import com.shark.erp.mapper.PurchaseDetailMapper;
import com.shark.erp.mapper.PurchaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurchaseDetailService {


    @Autowired
    PurchaseDetailMapper purchaseDetailMapper;

    public JSONObject selectAllPurchaseDetail(String manifestNumber) {
        JSONObject result = new JSONObject();
        try {
            List<PurchaseDetail> purchaseDetailList = purchaseDetailMapper.selectAllPurchaseDetail(manifestNumber);
            result.put("code", 0);
            result.put("msg", "查询成功");
            result.put("count", purchaseDetailList.size());
            result.put("data", purchaseDetailList);
        } catch (Exception ex) {
            result.put("code", 500);
            result.put("msg", "error");
        }
        return result;
    }

    public JSONObject selectPurchaseDetailByPurchaseDetailNumber(String name,String manifestNumber) {
        JSONObject result = new JSONObject();
        try {
            List<PurchaseDetail> purchaseDetailList = purchaseDetailMapper.selectPurchaseDetailByPurchaseDetailNumber(name,manifestNumber);
            result.put("code", 0);
            result.put("msg", "查询成功");
            result.put("count", purchaseDetailList.size());
            result.put("data", purchaseDetailList);
        } catch (Exception ex) {
            result.put("code", 500);
            result.put("msg", "error");
        }
        return result;
    }

    public JSONObject insertPurchaseDetail(PurchaseDetail purchaseDetail) {
        JSONObject result = new JSONObject();
        try {
            purchaseDetailMapper.insertPurchaseDetail(purchaseDetail);
            result.put("code", 0);
            result.put("msg", "插入成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            result.put("code", 500);
            result.put("msg", "error");
        }
        return result;
    }

    public JSONObject deletePurchaseDetail(String purchaseDetailNumber) {
        JSONObject result = new JSONObject();
        try {
            purchaseDetailMapper.deletePurchaseDetail(purchaseDetailNumber);
            result.put("code", 0);
            result.put("msg", "删除成功");
        } catch (Exception ex) {
            result.put("code", 500);
            result.put("msg", "error");
        }
        return result;
    }

    public JSONObject updatePurchaseDetail(PurchaseDetail purchaseDetail) {
        JSONObject result = new JSONObject();
        try {
            purchaseDetailMapper.updatePurchaseDetail(purchaseDetail);
            result.put("code", 0);
            result.put("msg", "修改成功");
        } catch (Exception ex) {
            result.put("code", 500);
            result.put("msg", "error");
        }
        return result;
    }
}
